package org.example.ruoxing_admin_springboot3.service.resources;
import jakarta.annotation.Resource;
import org.example.ruoxing_admin_springboot3.dao.RoleDao;
import org.example.ruoxing_admin_springboot3.dao.RoleResourcesRelationDao;
import org.example.ruoxing_admin_springboot3.dao.SystemResourcesDao;
import org.example.ruoxing_admin_springboot3.dto.SystemResourcesDto;
import org.example.ruoxing_admin_springboot3.exceptionHandleUtils.ExceptionHandleUtils;
import org.example.ruoxing_admin_springboot3.httpResponseResult.ResponseResult;
import org.example.ruoxing_admin_springboot3.mapper.RoleMapper;
import org.example.ruoxing_admin_springboot3.mapper.RoleResourcesRelationMapper;
import org.example.ruoxing_admin_springboot3.mapper.SystemResourcesMapper;
import org.example.ruoxing_admin_springboot3.utils.StringValidatorUtils;
import org.example.ruoxing_admin_springboot3.utils.TimestampUtil;
import org.example.ruoxing_admin_springboot3.utils.UUIDUtil;
import org.example.ruoxing_admin_springboot3.viewmodel.ResourcesTreeBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SystemResourcesService {
    @Resource
    private SystemResourcesMapper systemResourcesMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleResourcesRelationMapper roleResourcesRelationMapper;
    /**
     * @description 查询系统资源
     * @author 莫若省
     * @createTime 2024/5/8
     */
    public ResponseResult<Object> getAllSystemResources() {
        try {
            List<SystemResourcesDao> systemResourcesDaoList = systemResourcesMapper.getAllSystemResources();
            //转为树形结构返回
            return ResponseResult.SUCCESS(ResourcesTreeBuilder.buildTree(systemResourcesDaoList));

        } catch (Exception e) {
            ExceptionHandleUtils.reportException(e, "查询系统资源失败!");
            return ResponseResult.ERROR();
        }
    }
    /**
     * @description 新增系统资源
     * @author 莫若省
     * @createTime 2024/5/8
     */
    public ResponseResult<Object> addSystemResources(SystemResourcesDto systemResourcesDto) {
        try {
            //如果有没有资源id则表示直接添加根级资源，否则添加子资源
            if(StringValidatorUtils.isEmpty(systemResourcesDto.getResourcesId())){
                systemResourcesDto.setIsRoot(true);
            }else{
                systemResourcesDto.setIsRoot(false);
                systemResourcesDto.setParentNodeId(systemResourcesDto.getResourcesId());
            }
            //生成资源id
            systemResourcesDto.setResourcesId(UUIDUtil.generateUpperCaseUUID());
            //新增创建时间
            systemResourcesDto.setCreateTime(TimestampUtil.getCurrentMillisTimestamp());
            int addCount =  systemResourcesMapper.addSystemResources(systemResourcesDto);
            if(addCount > 0){
                //查询当前总角色列表
                List<RoleDao> roleDaoList = roleMapper.selectRoleList();
                //循环更新角色的资源关联关系
                for (RoleDao roleDao : roleDaoList) {
                    //建立角色资源关联对象
                    RoleResourcesRelationDao roleResourcesRelationDao = new RoleResourcesRelationDao();
                    //设置关联id
                    roleResourcesRelationDao.setRelationId(UUIDUtil.generateUpperCaseUUID());
                    //设置关联表的角色id
                    roleResourcesRelationDao.setRoleId(roleDao.getRoleId());
                    //设置关联表的角色名称
                    roleResourcesRelationDao.setRoleName(roleDao.getRoleName());
                    //设置关联表的资源id
                    roleResourcesRelationDao.setResourcesId(systemResourcesDto.getResourcesId());
                    //设置关联表的资源名称
                    roleResourcesRelationDao.setResourcesName(systemResourcesDto.getTitle());
                    //设置关联表的创建时间
                    roleResourcesRelationDao.setCreateTime(TimestampUtil.getCurrentMillisTimestamp());
                    //设置关联当前角色的表启用状态
                    roleResourcesRelationDao.setChecked(false);
                    //新增角色资源关联关系
                    roleResourcesRelationMapper.insertRoleResourcesRelation(roleResourcesRelationDao);
                }
                return ResponseResult.SUCCESS();
            }else{
                return ResponseResult.WARN("新增资源失败!");
            }
        } catch (Exception e) {
            return ResponseResult.ERROR();
        }
    }
}
